# -*- coding: utf-8 -*-
'''
@datetime : 2022/6/9 18:35
@author   : zhangxp
@file     : celery.py
'''
from celery import Celery
import sys
import os
import platform


NEXTT_REDIS_TEST_HOST = os.getenv('NEXTT_REDIS_TEST_HOST', "192.168.12.139")
NEXTT_REDIS_PROD_HOST = os.getenv('NEXTT_REDIS_PROD_HOST', "192.168.8.9")

# vm = os.popen('systemd-detect-virt').read() if platform.system()!='Windows' else 'none'
# if '--port=5556' in sys.argv or vm.startswith('vmware'):
#     app = Celery('nextt',
#                 broker='redis://%s:6379/0' % NEXTT_REDIS_TEST_HOST,
#                 backend='redis://%s:6379/1' % NEXTT_REDIS_TEST_HOST,
#                 include=['nextt.tasks'])

import socket
if socket.gethostname().startswith('n08'):
    app = Celery('nextt',
                broker='redis://:foobared_1234@%s:6380/0' % NEXTT_REDIS_PROD_HOST,
                backend='redis://:foobared_1234@%s:6380/1' % NEXTT_REDIS_PROD_HOST,
                include=['nextt.tasks', 'nextt.tasks_gpu'])
else:
    app = Celery('nextt',
                broker='redis://:foobared_1234@%s:6380/0' % NEXTT_REDIS_TEST_HOST,
                backend='redis://:foobared_1234@%s:6380/1' % NEXTT_REDIS_TEST_HOST,
                include=['nextt.tasks', 'nextt.tasks_gpu'])

app.conf.task_routes = ([
    ('nextt.tasks.*', {'queue': 'celery'}),
    ('nextt.tasks_gpu.*', {'queue': 'gpu'}),
],)

app.conf.broker_transport_options = {
    'socket_keepalive': True,
    'socket_keepalive_options': {
        socket.TCP_KEEPIDLE: 30,
        socket.TCP_KEEPINTVL: 30,
        socket.TCP_KEEPCNT: 5,
    },
}

# Optional configuration, see the application user guide.
app.conf.update(
    timezone='Asia/Shanghai',
    result_expires=3600 * 24 * 7,
    worker_prefetch_multiplier=1,
    redis_backend_health_check_interval=30,
    redis_retry_on_timeout=True,
    redis_socket_keepalive=True,
)

if __name__ == '__main__':
    app.start()
